<template>
  <div class="header">
    <div class="column">证券简称</div>
    <div class="column right">上市日期</div>
  </div>
  <div
      style="width: 100% ;height: 180px; background-color:rgb(5, 25, 75) "
      @mouseenter="stopAutoPaging"
      @mouseleave="startAutoPaging"
  >
    <div v-for="item in tableData" :key="item.id">
      <div class="content">
        <div class="contentName contentInside">{{item.contentName}}</div>
        <div class="contentInside">
          <div class="ref">{{ formatRaf(item.todayRaf) }}</div>
          <el-icon :color="item.todayRaf < 0 ? '#e4362d' : 'green'" :size="25" >
            <Top v-if="item.todayRaf >= 0" />
            <Bottom v-else/>
          </el-icon>
        </div>
      </div>
    </div>
  </div>
  <div class="block">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>
<script>
import needTools from "@/utils/needTools.js";
import {Bottom, Top} from "@element-plus/icons-vue";

export default {
  name: "Plate",
  components: {Bottom, Top},
  data() {
    return {
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 5,
      },
      total: 0,
      pagetotal: 0,
      currentPage: 0,
      autoLoadTimerId: null,
      autoPagingTimerId: null
    };
  },
  created() {
    this.load();
    this.startAutoPaging();
    this.autoLoadTimerId = setInterval(this.load, 10000);
  },
  beforeDestroy() {
    clearInterval(this.autoLoadTimerId);
    clearInterval(this.autoPagingTimerId);
  },
  methods: {
    formatRaf(cellValue) {
      return `${Math.abs(cellValue).toFixed(2)}%`;
    },
    load() {
      needTools.get("/fallUp/list", { params: this.params })
          .then(res => {
            if (res.code === 200) {
              this.tableData = res.data.records;
              this.total = res.data.total;
              this.pagetotal = Math.ceil(this.total / this.params.pageSize);
              this.currentPage = this.params.pageNum;
            } else {
              this.$message.error(res.msg);
            }
          });
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.load();
    },
    startAutoPaging() {
      this.autoPagingTimerId = setInterval(() => {
        this.params.pageNum = this.params.pageNum >= this.pagetotal ? 1 : this.params.pageNum + 1;
        this.load();
      }, 3000);
    },
    stopAutoPaging() {
      clearInterval(this.autoPagingTimerId);
    }
  }
};
</script>

<style scoped>
.column, .column right{
  color: #bbbdc6;
  font-size: 14px;
  display: inline-block;
  width: 50%;
  height: 25px;
  line-height: 25px;
  background-color: rgba(4, 59, 154,0.2);
  margin-bottom: 10px;
}

.column{
  padding-left: 20px;
}

.column right{
  text-align: right;
  padding-right: 30px;
}

.content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px;
  background-color: rgba(4, 59, 154, 0.2);
  color: #bbbdc6;
  font-size: 14px;
}

.contentInside {
  flex: 0 0 50%;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}

.ref {
  flex: 1;
  margin-right: 10px;
  color: #2aaee9;
  text-align: right;
}

:deep(.el-pagination) {
  margin-top: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-pagination button),
:deep(.el-pagination ul li) {
  background: rgb(5, 23, 77);
  color: white;
}

:deep(.el-pagination .is-first) ,
:deep(.el-pagination .is-last){
  background-color: rgb(5, 23, 77) !important;
  color: white;
  cursor: pointer;
}
</style>